package com.dj.mingkong.portal.biz.repository.database.music;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dj.mingkong.common.infrastructure.base.dto.query.QueryVo;
import com.dj.mingkong.common.infrastructure.base.eo.BaseEo;
import com.dj.mingkong.common.infrastructure.base.repository.BaseDatabaseRepository;
import com.dj.mingkong.common.infrastructure.eo.database.music.BoxEo;
import com.dj.mingkong.portal.biz.controller.music.vo.request.BoxQueryVo;
import com.dj.mingkong.portal.biz.repository.database.music.mapper.BoxMapper;
import com.dj.mingkong.portal.biz.util.WrapperUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;


/**
 * 音乐盒repository
 *
 * @author zenglonghui
 * @date: 2022/8/31 14:47
 */
@Repository
public class BoxRepository extends BaseDatabaseRepository<BoxMapper, BoxEo, QueryVo> {
    public boolean existByNameAndUserId(String name, Long userId) {
        return lambdaQuery().eq(BoxEo::getName, name).eq(BoxEo::getUserId, userId).exists();
    }

    public List<BoxEo> listByUserId(Long userId) {
        return lambdaQuery().eq(BoxEo::getUserId, userId).orderByAsc(BaseEo::getCreateTime).list();
    }

    public Page<BoxEo> listPageByVo(BoxQueryVo queryVo) {
        final QueryWrapper<BoxEo> queryWrapper = Wrappers.query();
        queryWrapper.lambda().eq(BoxEo::getUserId, queryVo.getUserId())
                .like(StringUtils.isNotBlank(queryVo.getName()), BoxEo::getName, queryVo.getName())
                .ge(StringUtils.isNotBlank(queryVo.getStartDate()), BoxEo::getCreateTime, queryVo.getStartDate())
                .le(StringUtils.isNotBlank(queryVo.getEndDate()), BoxEo::getCreateTime, queryVo.getEndDate())
                .orderByAsc(BoxEo::getType);
        WrapperUtil.buildSort(queryWrapper, queryVo.getSort());
        return listPage(queryWrapper, queryVo.getPage(), queryVo.getPageSize());
    }
}
